<template>
  <!-- 新增部门的弹层 -->
  <el-dialog :title="showTitle" :visible="showDialog" @close='btnCancel'>
    <!-- 表单组件  el-form   label-width设置label的宽度   -->
    <!-- 匿名插槽 -->
    <el-form :model="formData" :rules="rules" label-width="120px" ref="deptForm">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="formData.name" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input style="width:80%" placeholder="1-50个字符"  v-model="formData.code"/>
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select @focus="getEmployeeSimple" style="width:80%" placeholder="请选择" v-model="formData.manager">
          <el-option v-for="item in peoples" :key="item.id" :label="item.username" :value="item.username"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="formData.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
      </el-form-item>
    </el-form>
    <!-- el-dialog有专门放置底部操作栏的 插槽  具名插槽 -->
    <el-row slot="footer" type="flex" justify="center">
      <!-- 列被分为24 -->
      <el-col :span="6">
        <el-button type="primary" size="small" @click="btnOk">确定</el-button>
        <el-button size="small" @click="btnCancel">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>
<script>
import { getDepartments, addDepartments, getDepartDetail, updateDepartments } from '@/api/departments'
import { getEmployeeSimple } from '@/api/employess'
export default {
   props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    treeNode: {
      type: Object,
      default: null
    }
  },
  data() {
    // 检查部门名称是否重复
      const checkNameRepeat = async(rule, value, callback) => {
            // 获取最新的组织架构
          const { depts } = await getDepartments()
          let isRepeat = false
          if (this.formData.id) {
            // 有id就是编辑模式
            isRepeat = depts.filter(item => item.id !== this.formData.id && item.pid === this.treeNode.pid).some(item => item.name === value)
          } else {
            // 没有id就是新增模式
             isRepeat = depts.filter(item => item.pid === this.treeNode.id).some(item => item.name === value)
          }
          isRepeat ? callback(new Error(`同级部门下已经存在这个${value}部门了`)) : callback()
      }
      const checkCodeRepeat = async(rule, value, callback) => {
            // 获取最新的组织架构
          const { depts } = await getDepartments()
          let isRepeat = false
          if (this.formData.id) {
            // 有id就是编辑模式
            isRepeat = depts.some(item => item.id !== this.formData.id && item.code === value && value)
          } else {
            // 没有id就是新增模式
            isRepeat = depts.some(item => item.code === value && value)
          }
           isRepeat ? callback(new Error(`组织架构下已经存在这个${value}编码了`)) : callback()
      }
    return {
      // 定义表单数据
      formData: {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      },
      // 校验规则对象
      rules: {
         name: [{ required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: 'blur' }, { trigger: 'blur', validator: checkNameRepeat }],
        code: [{ required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: 'blur' }, { trigger: 'blur', validator: checkCodeRepeat }],
        manager: [{ required: true, message: '部门负责人不能为空', trigger: 'blur' }],
        introduce: [{ required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { trigger: 'blur', min: 1, max: 300, message: '部门介绍要求1-50个字符' }]
      },
      peoples: []
    }
  },
  methods: {
   async getEmployeeSimple () {
     this.peoples = await getEmployeeSimple()
    },
    // 获取详情的方法
   async getDepartDetail (id) {
    this.formData = await getDepartDetail(id)
    },
    btnOk () {
      this.$refs.deptForm.validate(async isOk => {
        if (isOk) {
          if (this.formData.id) {
            // 编辑模式
            await updateDepartments(this.formData)
          } else {
            // 新增模式
          await addDepartments({ ...this.formData, pid: this.treeNode.id })
          }
          // 告诉父组件
          this.$emit('addDepts') // 触发自定义事件
          // 关闭弹层 update： props属性
          this.$emit('update:showDialog', false)
        }
      })
    },
    // 点击关闭弹层
    btnCancel () {
      // 重置数据
      this.formData = {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      }
      // 关闭弹层 update： props属性
          this.$emit('update:showDialog', false)
          // 清空表单验证,resetFields只能清除data中的数据
          this.$refs.deptForm.resetFields()
    }
  },
  computed: {
    showTitle () {
      return this.formData.id ? '编辑部门' : '新增子部门'
    }
  }
}
</script>
